package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizChannelType;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizChannelTypeMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizChannelTypeParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizChannelTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 通道类型 服务实现类
 *
 * @author ssy
 * @date 2022/02/16 10:59
 */
@Service
public class BizChannelTypeServiceImpl extends ServiceImpl<BizChannelTypeMapper, BizChannelType> implements BizChannelTypeService {

    @Override
    public PageResult<BizChannelType> page(BizChannelTypeParam bizChannelTypeParam) {

        // 构造条件
        LambdaQueryWrapper<BizChannelType> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BizChannelType> list(BizChannelTypeParam bizChannelTypeParam) {

        // 构造条件
        LambdaQueryWrapper<BizChannelType> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(BizChannelTypeParam bizChannelTypeParam) {

        // 校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(bizChannelTypeParam, false);

        // 将dto转为实体
        BizChannelType bizChannelType = new BizChannelType();
        BeanUtil.copyProperties(bizChannelTypeParam, bizChannelType);

        this.save(bizChannelType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizChannelTypeParam bizChannelTypeParam) {
        this.removeById(bizChannelTypeParam.getId());
    }

    @Override
    public void edit(BizChannelTypeParam bizChannelTypeParam) {

        // 根据id查询实体
        BizChannelType bizChannelType = this.queryBizChannelType(bizChannelTypeParam);

        // 校验参数，检查是否存在重复的编码或者名称，排除当前编辑的这条记录
        checkParam(bizChannelTypeParam, true);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizChannelTypeParam, bizChannelType);

        this.updateById(bizChannelType);
    }

    @Override
    public BizChannelType detail(BizChannelTypeParam bizChannelTypeParam) {
        return this.queryBizChannelType(bizChannelTypeParam);
    }


    /**
     * 校验参数，检查是否存在重复的编码等
     *
     * @author ssy
     * @since 2022/02/16 10:59
     */
    private void checkParam(BizChannelTypeParam bizChannelTypeParam, boolean isExcludeSelf) {
        Integer id = bizChannelTypeParam.getId();
        String code = bizChannelTypeParam.getCode();

        // 构建带code的查询条件
        LambdaQueryWrapper<BizChannelType> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(BizChannelType::getCode, code);

        // 如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByCode.ne(BizChannelType::getId, id);
        }
    }

    /**
     * 获取通道类型
     *
     * @author ssy
     * @since 2022/02/16 10:59
     */
    private BizChannelType queryBizChannelType(BizChannelTypeParam bizChannelTypeParam) {
        BizChannelType bizChannelType = this.getById(bizChannelTypeParam.getId());
        return bizChannelType;
    }
}
